package com.mhb.generator.config.builder;

import com.mhb.generator.config.*;
import com.mhb.generator.config.po.TableInfo;
import com.mhb.generator.config.rules.FileType;
import com.mhb.generator.config.rules.UseViewType;
import com.mhb.generator.engine.FreemarkerTemplateEngine;
import com.mhb.generator.engine.TemplateAbstract;
import com.mhb.generator.util.ExceptionUtils;
import com.mhb.generator.util.StringUtil;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

/**
 * @author 博博
 * @Title: ConfigBuilder
 * @time 2019/12/28 0028 17:53
 */
@Data
@Accessors(chain = true)
public class ConfigBuilder {
    /**
     * 总和配置
     */
    private AutoGenerator autoGenerator;

    /**
     * 包信息
     */
    private final Map<String, String> pathInfo = new HashMap<>();

    public ConfigBuilder(AutoGenerator autoGenerator, List<TableInfo> tableInfos) {
        this.autoGenerator = autoGenerator;
        GlobalConfig globalConfig = autoGenerator.getGlobalConfig();
        // 文件路径
        String filePath = globalConfig.getFilePath();

        pathInfo.put(ConstantVal.ENTITY_PATH, filePath + autoGenerator.parentPackageAndChildPath(autoGenerator.getPackageConfig().getEntity()));
        // 存入文件包名路径
        if (globalConfig.isGenerateMapper()) {
            pathInfo.put(ConstantVal.MAPPER_PATH, filePath + autoGenerator.parentPackageAndChildPath(autoGenerator.getPackageConfig().getMapper()));
            pathInfo.put(ConstantVal.XML_PATH, filePath + autoGenerator.parentPackageAndChildPath(autoGenerator.getPackageConfig().getMapper()));
        }
        // controller
        if (globalConfig.isGenerateController()) {
            pathInfo.put(ConstantVal.CONTROLLER_PATH, filePath + autoGenerator.parentPackageAndChildPath(autoGenerator.getPackageConfig().getController()));
        }
        // service
        if (globalConfig.isGenerateService()) {
            pathInfo.put(ConstantVal.SERVICE_PATH, filePath + autoGenerator.parentPackageAndChildPath(autoGenerator.getPackageConfig().getService()));
            pathInfo.put(ConstantVal.SERVICE_IMPL_PATH, filePath + autoGenerator.parentPackageAndChildPath(autoGenerator.getPackageConfig().getService()) + "//impl");
        }
        // view
        if (globalConfig.isGenerateTemplate()) {
            pathInfo.put(ConstantVal.TEMPLATE_PATH, autoGenerator.getViewConfig().getTemplatePath());
        }

        TemplateAbstract templateAbstract = null;
        // 如果使用freemarker模板
        if (this.autoGenerator.getViewConfig().getUseViewType().equals(UseViewType.FTL)) {
            templateAbstract = new FreemarkerTemplateEngine();
        }

        templateAbstract.setConfigBuilder(this).mkdirs().generate(tableInfos).open();
    }

    public String getPath(String path, String createName, String suffix) {
        return StringUtil.format("%s\\%s.%s", path, createName, suffix);
    }

}
